package com.yunxin.customui.service;

import com.yunxin.customui.util.PageUtil;
import com.yunxin.customui.bean.UIComponentConfig;
import com.yunxin.customui.myinterface.IEventHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 事件处理业务
 *
 * @author lee
 * @date 2021/10/07
 */
@Component
public class EventService {

    @Autowired
    ApplicationContext ap;

    /**
     * 事件处理
     *
     * @param uiConfig   用户界面配置
     * @param eventParam 事件参数
     */
    public List<IEventHandler.EventHandlerResult> eventHandle(UIComponentConfig uiConfig,Object eventParam){
        String eventHandler = uiConfig.getEventHandler();
        List<PageUtil.MethodParam> methodParams = PageUtil.analysisMethod(eventHandler);
        List<IEventHandler.EventHandlerResult> resultList = new ArrayList<>();
        for (PageUtil.MethodParam methodParam : methodParams) {
            String methodName = methodParam.getMethodName();
            IEventHandler handler = (IEventHandler) ap.getBean(methodName);
            resultList.addAll(handler.eventHandle(uiConfig, eventParam));
        }

        return resultList;
    }


}
